package com.redpacketcover.rc;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mob.MobSDK;
import com.redpacketcover.rc.base.BasicActivity;
import com.redpacketcover.rc.bean.GoodsRecord;
import com.redpacketcover.rc.common.TitleBar;
import com.redpacketcover.rc.goods.GoodsPresenter;
import com.redpacketcover.rc.goods.IMatchView;
import com.redpacketcover.rc.tabbar.TabCardFragment;
import com.redpacketcover.rc.tabbar.TabGoodsFragment;
import com.redpacketcover.rc.tabbar.TabMyFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BasicActivity implements View.OnClickListener {

    private TitleBar mtitlebar;
    //声明ViewPager
    private ViewPager mViewPager;
    //适配器
    private FragmentPagerAdapter mAdapter;
    //装载Fragment的集合
    private List<Fragment> mFragments;

    //四个Tab对应的布局
    private LinearLayout mTabGoods;
    private LinearLayout mTabCard;
    private LinearLayout mTabMy;

    //四个Tab对应的ImageButton
    private ImageButton mImgGoods;
    private ImageButton mImgCard;
    private ImageButton mImgMy;

    private TextView mTextGoods;
    private TextView mTextCard;
    private TextView mTextMy;

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected GoodsPresenter createPresenter() {
        return null;
    }

    @Override
    protected void initViews() {
        MobSDK.init(this);
        mtitlebar = findViewById(R.id.title_bar);
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);

        mTabGoods = (LinearLayout) findViewById(R.id.id_tab_vote);
        mTabCard = (LinearLayout) findViewById(R.id.id_tab_frd);
        mTabMy = (LinearLayout) findViewById(R.id.id_tab_detail);

        mImgGoods = (ImageButton) findViewById(R.id.id_tab_vote_img);
        mImgCard = (ImageButton) findViewById(R.id.id_tab_frd_img);
        mImgMy = (ImageButton) findViewById(R.id.id_tab_detail_img);

        mTextGoods = findViewById(R.id.id_tab_vote_text);
        mTextCard = findViewById(R.id.id_tab_frd_text);
        mTextMy = findViewById(R.id.id_tab_detail_text);
    }

    @Override
    protected void initData() {
        getResources().getDimensionPixelSize(R.dimen.status_bar_height);
        mFragments = new ArrayList<>();
        //将四个Fragment加入集合中
        mFragments.add(new TabGoodsFragment(getSupportFragmentManager(),this));
        mFragments.add(new TabCardFragment(getSupportFragmentManager(),this));
        mFragments.add(new TabMyFragment(getSupportFragmentManager(),this));
        mtitlebar.setOnLeftIconClickListner(new TitleBar.OnLeftIconClickListener() {
            @Override
            public void onLeftIconClick() {
                finish();
            }
        });
        //初始化适配器
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager(),FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public Fragment getItem(int position) {//从集合中获取对应位置的Fragment
                return mFragments.get(position);
            }

            @Override
            public int getCount() {//获取集合中Fragment的总数
                return mFragments.size();
            }

        };
        mTabGoods.setOnClickListener(this);
        mTabCard.setOnClickListener(this);
        mTabMy.setOnClickListener(this);
        mtitlebar.setLeftIconVisible(View.GONE);
        mtitlebar.setBackgroundColor(Color.parseColor("#0082FF"));
        //不要忘记设置ViewPager的适配器
        mViewPager.setAdapter(mAdapter);
        //设置ViewPager的切换监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            //页面滚动事件
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //页面选中事件
            @Override
            public void onPageSelected(int position) {
                //设置position对应的集合中的Fragment
                if(position==0){
                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //显示状态栏
                    mtitlebar.setVisibility(View.VISIBLE);
                    mtitlebar.setTitle("炫年货");
                }else if(position == 1){
                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //显示状态栏
                    mtitlebar.setVisibility(View.VISIBLE);
                    mtitlebar.setTitle("智名片");
                }else{
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
                    mtitlebar.setVisibility(View.GONE);
//                    setHideStatus();
                }
                mViewPager.setCurrentItem(position);
                resetImgs();
                selectTab(position);
            }

            @Override
            //页面滚动状态改变事件
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        //先将四个ImageButton置为灰色
        resetImgs();

        //根据点击的Tab切换不同的页面及设置对应的ImageButton为绿色
        switch (v.getId()) {
            case R.id.id_tab_vote:
                selectTab(0);
                break;
            case R.id.id_tab_frd:
                selectTab(1);
                break;
            case R.id.id_tab_detail:
                selectTab(2);
                break;
        }
    }

    private void selectTab(int i) {
        //根据点击的Tab设置对应的ImageButton为绿色
        switch (i) {
            case 0:
                mImgGoods.setImageResource(R.mipmap.home_1);
                mTextGoods.setTextColor(Color.parseColor("#333333"));
                break;
            case 1:
                mImgCard.setImageResource(R.mipmap.card_1);
                mTextCard.setTextColor(Color.parseColor("#333333"));
                break;
            case 2:
                mImgMy.setImageResource(R.mipmap.user_1);
                mTextMy.setTextColor(Color.parseColor("#333333"));
                break;
        }
        //设置当前点击的Tab所对应的页面
        mViewPager.setCurrentItem(i);
    }

    //将四个ImageButton设置为灰色
    private void resetImgs() {
        mImgGoods.setImageResource(R.mipmap.home_0);
        mImgCard.setImageResource(R.mipmap.card_0);
        mImgMy.setImageResource(R.mipmap.user_0);
        mTextMy.setTextColor(Color.parseColor("#999999"));
        mTextCard.setTextColor(Color.parseColor("#999999"));
        mTextGoods.setTextColor(Color.parseColor("#999999"));
    }
}